<?php

use app\event\OldAgent;
use think\migration\Migrator;
use app\model\Agent;
use app\model\PageSetting;

class AlterOldAgentInit extends Migrator
{
    public function up()
    {
        $agents = $this->agent();
        if (!$agents->isEmpty()) {
            $agents->each(function (Agent $agent) {
                /**
                 * @var Agent
                 */
                event(new OldAgent($agent));
            });
        }
    }

    public function down()
    {
        $agents = $this->agent();
        if (!$agents->isEmpty()) {
            $agents->each(function (Agent $agent) {
                PageSetting::destroy(function($query) use ($agent){
                    $query->where('agent_id',$agent->id)->where('source', PageSetting::SOURCE_ZDY);
                });
            });
        }
    }

    public function agent()
    {
        return Agent::query()->where('is_deleted', 0)->select();
    }
}
